﻿#region

using System;
using System.Security.Cryptography;
using System.Text;

#endregion

/**************************
 * NOTE: This code was taken from Jon Galloway's
 *  article on ASP.NET: http://weblogs.asp.net/jgalloway/archive/2008/04/13/encrypting-passwords-in-a-net-app-config-file.aspx
 *  
 * This code has been modified to use normal strings, instead of SecureStrings - since I'm not too concerned about the password being read from memory
 **************************/
namespace JetLoginTool.Engine
{
	/// <summary>
	/// Handles the encryption and decryption of the password
	/// </summary>
	public static class EncryptionHandler
	{
		private static readonly byte[] entropy = Encoding.Unicode.GetBytes("Salt Is Not A Password");

		public static string EncryptString(string input)
		{
			byte[] encryptedData = ProtectedData.Protect(
				Encoding.Unicode.GetBytes(input),
				entropy,
				DataProtectionScope.CurrentUser);
			return Convert.ToBase64String(encryptedData);
		}

		public static string DecryptString(string encryptedData)
		{
			string outString = "";
			try
			{
				byte[] decryptedData = ProtectedData.Unprotect(
					Convert.FromBase64String(encryptedData),
					entropy,
					DataProtectionScope.CurrentUser);
				outString = Encoding.Unicode.GetString(decryptedData);
			}
			catch
			{}

			return outString;
		}
	}
}
